package com.bds.tjj.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bds.tjj.model.ConfidenData;
import com.bds.tjj.model.MonthData;
import com.bds.tjj.model.News;
import com.bds.tjj.model.User;
import com.bds.tjj.model.YearData;
import com.bds.tjj.service.ConfidenSumService;
import com.bds.tjj.service.MonthSumService;
import com.bds.tjj.service.NewsService;
import com.bds.tjj.service.UserService;
import com.bds.tjj.service.YearSumService;

@Controller
public class IndexController extends BaseAction {
	// 默认每10mb生成一个log文件
	Logger log = LoggerFactory.getLogger(getClass());
	// @Value("${application.hello:Hello Angel}")
	@Autowired
	private YearSumService yearSumService;

	@Autowired
	private MonthSumService monthSumService;
	@Autowired
	private NewsService newsService;
	@Autowired
	private UserService userService;
	@Autowired
	private ConfidenSumService confidenSumService;

	@RequestMapping("/")
	public String index(Map<String, Object> map, HttpServletRequest request, String username, String v) {
		map.put("username", username);
		try {
			YearData index1 = yearSumService.getYearNum("index1");

			if (index1 != null) {
				map.put("gdp", index1);

			}

			MonthData index3 = monthSumService.getMonthNum("t39");

			if (index3 != null) {

				map.put("cpi", index3);

			}
			ConfidenData index9 = confidenSumService.getConfidenMonthNum("index9");

			if (index9 != null) {
				map.put("cci", index9);

			}

			MonthData index6 = monthSumService.getMonthNum("index6");

			if (index6 != null) {
				map.put("pmi", index6);

			}

			List<News> news = newsService.getLastedNews(5);
			if (news != null) {
				map.put("news", news);
			} else {
				map.put("news", new ArrayList<News>());
			}

			List<MonthData> ms = newsService.getLastedIndex(5);
			map.put("lastms", ms);
			// SimpleDateFormat ymFormat = new SimpleDateFormat("MMM",
			// Locale.ENGLISH);
			// SimpleDateFormat yyFormat = new SimpleDateFormat("yyyy",
			// Locale.ENGLISH);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 判断是否登陆
		/*
		 * HttpSession session = request.getSession(); if
		 * (session.getAttribute(UserSession.sessionKey) != null) { return
		 * "ftl/index"; }
		 */
		if (getUserSession(request) != null) {
			return "ftl1/index";
		} else {
			return "redirect:login.html";

		}

	}

//	@RequestMapping("/analytics/month_data")
//	public String analyticsMonthData(Map<String, Object> map) {
//		return "ftl/analytics/month_data";
//	}
//
//	@RequestMapping("/analytics/year_data")
//	public String analyticsYearData(Map<String, Object> map) {
//		return "ftl/analytics/year_data";
//	}

	@RequestMapping("/analytics/data")
	public String analyticsData(Map<String, Object> map, String v) {
		return "ftl1/analytics/data";
	}

	// 个人信息
	@RequestMapping("/data/user/user_profile")
	public String user_profile(Map<String, Object> map, HttpServletRequest request, String v) {
		User user = userService.getCurrentUserInfoBySession(request);
		if (user == null) {
			user = userService.getCurrentUserInfo(request);
		}
		String username = user.getUsername();
		String phone_number = user.getPhone_number();
		String email = user.getEmail();
		if (username == null) {
			username = "";
		}
		if (phone_number == null) {
			phone_number = "";
		}
		if (email == null) {
			email = "";
		}

		map.put("username", username);
		map.put("phone_number", phone_number);
		map.put("email", email);

		return "ftl1/analytics/user_profile";

	}

	@RequestMapping("/data/user/user_pwd")
	public String user_pwd(Map<String, Object> map, HttpServletRequest request, String v) {
		User user = userService.getCurrentUserInfoBySession(request);
		if (user == null) {
			user = userService.getCurrentUserInfo(request);
		}
		String username = user.getUsername();
		String phone_number = user.getPhone_number();
		String email = user.getEmail();
		if (username == null) {
			username = "";
		}
		if (phone_number == null) {
			phone_number = "";
		}
		if (email == null) {
			email = "";
		}

		map.put("username", username);
		map.put("phone_number", phone_number);
		map.put("email", email);

		return "ftl1/analytics/user_pwd";
	}

	@RequestMapping("/user/lock")
	public String lock(Map<String, Object> map, HttpServletRequest request, String v) {
		User user = userService.getCurrentUserInfoBySession(request);
		if (user == null) {
			user = userService.getCurrentUserInfo(request);
		}
		String username = user.getUsername();
		String email = user.getEmail();
		if (username == null) {
			username = "";
		}
		if (email == null) {
			email = "";
		}

		map.put("username", username);
		map.put("email", email);
		return "ftl1/lock";
	}

	@RequestMapping("/analytics/tab_detail")
	public String tabDetail(Map<String, Object> map, HttpServletRequest request, String v) {
		return "ftl1/analytics/data_tab";
	}

}
